package com.example.superoa.controller;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import java.lang.Integer;
import java.math.BigDecimal;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.superoa.model.pojos.SalaryAccout;
import com.example.superoa.model.pojos.SalaryItem;
import com.example.superoa.model.service.AccountService;
import com.example.superoa.model.service.SalaryStaffService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;


@Controller
public class AccountController {
	@Autowired
	private AccountService ss;
	
	@Autowired
	private SalaryStaffService biz;
	
	//分页查询所有套账
	@RequestMapping("showaccount")
	@ResponseBody
	public Map<String,Object> show(int page,int rows) {
		Map<String,Object> map = new HashMap<>();
			//先分页
		Page<Object> pg = PageHelper.startPage(page, rows);
			
		JSONArray i=ss.findAll();
		for (Object object : i) {
			JSONObject obj=(JSONObject)object;
			JSONArray j = ss.selectId1(Integer.parseInt(obj.get("account_id").toString()));
			String str="";
			for (int k = 0; k < j.size(); k++) {
				JSONObject obj2=(JSONObject)j.get(k);
				if(k==j.size()-1) {
					str+=obj2.get("Staff_name");
				}else {
						str+=obj2.get("Staff_name")+",";
				}
					
			}
			obj.put("Staff_name", str);
		}
		//总记录
		map.put("total", pg.getTotal());
		//页内容
		map.put("rows", i);
		return map;
	}
	//select显示
	@RequestMapping("findall-salary")
	@ResponseBody
	public JSONArray findAll() {
		return ss.findAll();
	}
	//通过套账名称查询
	@RequestMapping("find-name")
	@ResponseBody
	public Map<String,Object> findNames(int account_id,int page,int rows) {
		Map<String,Object> map = new HashMap<>();
		//先分页
		Page<Object> pg = PageHelper.startPage(page, rows);
			
		JSONArray s=ss.findName(account_id);
		//总记录
		map.put("total", pg.getTotal());
		//页内容
		map.put("rows", s);
		return map;
	}
	
	//套账数据初始化
	@RequestMapping("salary-emp-add")
	public void add(Integer account_id,Model model) {
		JSONArray json=ss.bianji(account_id);
		String str="";
		for (int k = 0; k < json.size(); k++) {
		JSONObject obj2=(JSONObject)json.get(k);
			if(k==json.size()-1) {
				System.err.println(json.size());
				str+=obj2.get("Staff_name");
			}else {
				str+=obj2.get("Staff_name")+",";
			}
		}
		model.addAttribute("pojos", json.get(0));	
		model.addAttribute("str",str);
	}
	//套账查看表格
	@RequestMapping("account-table")
	@ResponseBody
	public JSONArray tables(int account_id) {
		JSONArray json = ss.finditems(account_id);
		int j=1;
		for (Object object : json) {
			JSONObject obj=(JSONObject) object;
			obj.put("xuhao", j);
			j++;
		}
		return json;
	}
	@RequestMapping("salary-emp-tianjia")
	public void tianjias() {
		
	}
	@PostMapping("account-xz")
	@ResponseBody
	public void tianjias(SalaryAccout sa,String tzyg_id,String accountName,BigDecimal saBase,String saRemarks,HttpSession session) {
		StringBuffer sb = new StringBuffer(tzyg_id);
		sb.deleteCharAt(sb.length()-1);
		sb.deleteCharAt(0);
		//分割
		List<Integer> ids = new ArrayList<>();
		String[] split = sb.toString().split(",");
		for (String s : split) {
			ids.add(Integer.parseInt(s));
		}
		Integer id=Integer.parseInt(session.getAttribute("sid").toString());
		ss.xinzeng(sa, id,ids,accountName,saBase,saRemarks);
		biz.stafft(ids);
	}
	@RequestMapping("salary-mingxi")
	public void mx(int account_id,Model model) {	
		model.addAttribute("pay",account_id);		
	}
	@RequestMapping("sm")
	@ResponseBody
	public JSONArray ms(int account_id) {
		return ss.paytable(account_id);		
	}
	@RequestMapping("salary-mingxis")
	@ResponseBody
	public Map<String,Object> mingxi(int account_id,int page,int rows) {
		Map<String,Object> map = new HashMap<>();
			//先分页
		Page<Object> pg = PageHelper.startPage(page, rows);
			
		JSONArray i=ss.paytable(account_id);
		
		//总记录
		map.put("total", pg.getTotal());
		//页内容
		map.put("rows", i);
		return map;
	}
	
	//通过员工名查询明细
	@RequestMapping("findname-pay")
	@ResponseBody
	public Map<String,Object> findpayName(String account_name,int page,int rows) {
		Map<String,Object> map = new HashMap<>();
		//先分页
		Page<Object> pg = PageHelper.startPage(page, rows);
				
		JSONArray s=ss.findpayName(account_name);
		//总记录
		map.put("total", pg.getTotal());
		//页内容
		map.put("rows", s);
		return map;
	}
	@RequestMapping("findsitems")
	@ResponseBody
	public JSONArray findsitems() {
		return ss.findsitems();
	}
	
	@PostMapping("add-items")
	@ResponseBody
	public void xzitems(int account_id,int items_id) {
		ss.additems(account_id, items_id);
	}
	//禁用
	@PostMapping("jinyong")
	@ResponseBody
	public void jinyong(int account_id) {
		ss.jinyong(account_id);
	}
	@PostMapping("sajingyong")
	@ResponseBody
	public JSONArray sa_jingyong() {
		return ss.sa_jingyong();
	}
	@RequestMapping("xzitemss")
	@ResponseBody
	public String xzitems(String items_name,BigDecimal items_money,String si_remarks,Integer account_id) {
		System.err.println(items_name+"-------"+"---------"+account_id+"------"+items_money+"---"+si_remarks);
		ss.xzitems(items_name,items_money,si_remarks,account_id);
		return account_id+"";
	}
}
